$(function () {
  //展示评论
  let id = localStorage.getItem('art_id')
  getcomments()

  function getcomments() {
    $.ajax({
      method: 'get',
      url: '/api/articles/' + id + '/comments',
      success(res) {
        if (res.status !== 0) {
          let html = template('nocomment', res)
          return $('.mycomment').html(html)
        }
        let html = template('commentlist', res)
        $('.mycomment').html(html)
      }
    })
  }

  // 发表评论
  $('#comment-form').on('submit', function (e) {
    e.preventDefault()
    let data = $(this).serialize()
    $.post('/api/articles/' + id + '/comments', data, (res) => {
      if (res.status !== 0) {
        return console.log(res.message);
      }
      getcomments()
      // that[0].reset()
      $(this)[0].reset()
    })
  })

})